package cn.getech.data.metadata.controller.meta;

import cn.getech.data.intelligence.common.utils.BaseRes;
import cn.getech.data.intelligence.common.utils.DataRes;
import cn.getech.data.intelligence.common.utils.PageUtils;
import cn.getech.data.intelligence.common.utils.R;
import cn.getech.data.intelligence.common.validator.ValidatorUtils;
import cn.getech.data.intelligence.common.validator.group.AddGroup;
import cn.getech.data.metadata.model.req.meta.CollectionTaskListReq;
import cn.getech.data.metadata.model.res.CollectionTaskLogDetail;
import cn.getech.data.metadata.service.CollectionTaskLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;


/**
 * @description: 采集任务
 * @author: wangzhaowen：kiss
 * @create: 2020/9/30 11:00
 * @version: 2020/9/30
 **/
@Slf4j
@Api(value="任务日志",tags={"任务日志"})
@RestController
@RequestMapping("md/task/log")
public class CollectionTaskLogController {
    @Autowired
    private CollectionTaskLogService collectionTaskLogService;

    @ApiOperation("日志列表")
    @PostMapping("/list")  // /data-metadata/md/schedule/log/list
    public R list(@RequestBody CollectionTaskListReq req){
        ValidatorUtils.validateEntity(req, AddGroup.class);
        PageUtils page = collectionTaskLogService.selectListPage(req);
        return R.okWithPage(page);
    }

    @ApiOperation("日志详情")
    @GetMapping("/info/{id}") // /data-metadata/md/schedule/log/info
    public DataRes<CollectionTaskLogDetail> info(@PathVariable("id") Long id) {
        CollectionTaskLogDetail collectionTask = collectionTaskLogService.getDetailById(id);
        return BaseRes.okWithData(collectionTask);
    }

    @ApiOperation("日志详情")
    @GetMapping("/version/{id}") // /data-metadata/md/task/log/version
    public  R version(@PathVariable("id")  Long id) {
        if (id == null) {
            return R.error("id 不能为空");
        }
        List<HashMap<String,Object>> data = collectionTaskLogService.selectVersionList(id);
        return R.okWithData(data);
    }

    @ApiOperation("日志对比")
    @PostMapping("/compare") // /data-metadata/md/task/log/compare
    public R compare(@RequestBody CollectionTaskListReq req) {
        R r = new R();
        if (req.getId() == null) {
            return R.error("id 不能为空");
        }
        if (req.getCompareId() == null) {
            return R.error("compareId 不能为空");
        }
        CollectionTaskLogDetail current = collectionTaskLogService.getDetailById(req.getId());
        CollectionTaskLogDetail compare = collectionTaskLogService.getDetailById(req.getCompareId());
        r.put("current", current);
        r.put("compare", compare);
        return r;
    }
}
